import axios from "axios";
import { getRandomInt, getTimeStamp, randomRangeInt } from "src/utils/common";

/**
 * @param artist 作者
 * @param duration 歌曲时长(秒)
 * @param musicrid 拼接后的歌曲id
 * @param rid 歌曲id
 * @param name 歌曲名称
 * @param pic 歌曲封面
 */
export interface kwSongItem {
  artist?: string;
  name?: string;
  rid?: number;
  musicrid?: string;
  pic: string;
  duration: number;
}

export interface wsSongItem {
  song: kwSongItem;
  time: number;
  user?: {
    user_name: string;
    user_id: number;
  };
}


/**
 *
 * @param key 用来搜索的内容
 */
export async function searchKwSong(key: string): Promise<kwSongItem[]> {
  const data = await axios.get(
    `http://bd.kuwo.cn/api/www/search/searchMusicBykeyWord?key=${encodeURI(
      key
    )}&httpsStatus=1&reqId=52f93060-9234-11ec-bdb3-f98d4d6cfeb5`,
    {
      headers: {
        Accept: "application/json, text/plain, */*",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.9",
        Connection: "keep-alive",
        Cookie:
          "_ga=GA1.2.1500987479.1595755923; _gid=GA1.2.568444838.1596065504; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1595755923,1596065505; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1596076178; kw_token=P5XA2TZXG9",
        csrf: "P5XA2TZXG9",
        Host: "www.kuwo.cn",
        "User-Agent":
          "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36",
        Referer: `http://www.kuwo.cn/search/list?${encodeURI(key)}`,
      },
    }
  );
  if (data.data.code != 200) {
    return [];
  }
  return data.data.data;
}
/**
 *
 * @param rid 歌曲rid
 * @returns 获取歌曲url
 */
export async function getKwSongUrl(rid: number): Promise<string> {
  try {
    const res = await axios.get(
      `http://antiserver.kuwo.cn/anti.s?type=convert_url&format=mp3&response=url&rid=MUSIC_${rid}`
    );
    return res.data;
  } catch (error) {
    return "";
  }
}
/**
 * 
 * @param bangId 榜单id
 * @returns 返回随机歌曲的信息
 */
export async function getKwRandSong(
  bangId: number
): Promise<wsSongItem | false> {
  try {
    const rand = randomRangeInt(10000000, 99999999);
    const res = await axios.get<{ data: { musicList: kwSongItem[] } }>(
      `http://kuwo.cn/api/www/bang/bang/musicList?bangId=${bangId}&pn=1&rn=100`,
      {
        headers: {
          accept: "application/json, text/plain, */*",
          "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
          csrf: rand,
          "sec-ch-ua":
            '" Not A;Brand";v="99", "Chromium";v="98", "Microsoft Edge";v="98"',
          "sec-ch-ua-mobile": "?0",
          "sec-ch-ua-platform": '"Windows"',
          "sec-fetch-dest": "empty",
          "sec-fetch-mode": "cors",
          "sec-fetch-site": "same-origin",
          cookie:
            "_ga=GA1.2.350831358.1644504872; _gid=GA1.2.452917979.1645287157; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1644504880,1645287157,1645287183,1645346622; h5Uuid=46b216feccca4ab080d4fdfa8b37e2-5c; _gat=1; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1645358694; kw_token=" +
            rand,
          Referer: "https://kuwo.cn/rankList",
          "Referrer-Policy": "strict-origin-when-cross-origin",
        },
      }
    );
    const { musicList } = res.data.data;
    if (!musicList.length) {
      return null;
    }
    const time = getTimeStamp();
    const randomId = getRandomInt(musicList.length);
    const song = musicList[randomId];
    return {
      time: time,
      song: {
        rid: song.rid,
        pic: song.pic,
        duration: song.duration,
        artist: song.artist,
        name: song.name,
      },
    };
  } catch (error) {
    return false;
  }
}
/**
 * 
 * @param rid 歌曲id
 * @returns 返回lrc歌词
 */
export async function getKwSongLrc(rid: number) {
  const res = await axios.get(
    `http://m.kuwo.cn/newh5/singles/songinfoandlrc?musicId=${rid}`,
    {
      headers: {
        Cookie:
          "_ga=GA1.2.1500987479.1595755923; _gid=GA1.2.568444838.1596065504; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1595755923,1596065505; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1596076178; kw_token=P5XA2TZXG9",
        csrf: "P5XA2TZXG9",
      },
    }
  );
  return res.data.data;
}
